﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1.DesignerPatter.Test
{
    class TestObServer
    {
        static void Main()
        {
            Secretary litong = new Secretary();
            litong.SubjectState = "老板来了，";
            MyObserver o1 = new MyObserver("李世杰", litong);
            NbaOberserver o2 = new NbaOberserver("王杰", litong);
            litong.Update +=new Secretary.EventHandle(o1.CloseStock);
            litong.Update += new Secretary.EventHandle(o2.CloseNba);
            //litong.Attach(o1);
            //litong.Attach(o2);
            litong.Notify();
            Console.Read();
        }
    }

    //abstract class Oberserver
    //{
    //    protected string name;
    //    protected Secretary sub;

    //    public Oberserver(string name, Secretary sec)
    //    {
    //        this.name = name;
    //        this.sub = sec;
    //    }

    //    public abstract void Update();
    //}

    interface Subject
    {
        void Notify();
        string SubjectState
        {
            get;
            set;
        }
    }

    class MyObserver
    {
        string name;
        Subject sub;
        public MyObserver(string name, Subject sec)
        {
            this.name = name;
            this.sub = sec;
        }
        public  void  CloseStock()
        {
            Console.WriteLine("{0} {1} 关闭股票行情",this.sub.SubjectState,this.name);
        }
    }

    class NbaOberserver 
    {
        string name;
        Subject sub;
        public NbaOberserver(string name, Subject sub)
        {
            this.name = name;
            this.sub = sub;
        }
        public  void CloseNba()
        {
            Console.WriteLine("{0}{1} 关闭nba",this.sub.SubjectState,this.name);
        }
    }

    class Secretary : Subject
    {
        public delegate void EventHandle();
        public event EventHandle Update;
        private string action;
        public string SubjectState
        {
            get { return action; }
            set { action = value; }
        }

        public void Notify()
        {
            Update();
        }
    }
}
